//
//  CacheTool.m
//  CacheFunction
//
//  Created by MS on 15/4/23.
//  Copyright (c) 2015年 Sunshine. All rights reserved.
//  缓存相关

#import "CacheTool.h"
#import "Utils.h"
#import "FunctionItem.h"


static  NSString * const functionCategory = @"function-category";

@implementation CacheTool

/**
 *  获取用户收藏的分类
 */
+(NSArray*)loadUserFunctionCategory
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    
    NSArray *dataArr = [defaults objectForKey:functionCategory];
    
    NSMutableArray * tempArray = [NSMutableArray array];
    
    [dataArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [tempArray addObject:[NSKeyedUnarchiver unarchiveObjectWithData:obj]];
    }];
    
    return tempArray;
}
/**
 *  保存用户的收藏的分类
 */
+(BOOL)saveUserFunctionCategory:(NSArray*)dataArray
{
    NSMutableArray * tempArray = [NSMutableArray array];
    
    [dataArray enumerateObjectsUsingBlock:^(FunctionItem* item, NSUInteger idx, BOOL * _Nonnull stop) {
        NSData * data = [NSKeyedArchiver archivedDataWithRootObject:item];
        [tempArray addObject:data];
    }];
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    
    NSArray *array = [NSArray arrayWithArray:tempArray];
    
    [defaults setObject:array forKey:functionCategory];
    
    return [defaults synchronize];
}



+(NSDictionary*)loadDataFromCommonPlistByKey:(NSString*)key
{
    //读取资源文件名称为common.plist
    NSBundle *bundle = [NSBundle mainBundle];
    
    NSString *path = [bundle pathForResource:@"common" ofType:@"plist"];
    
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    
    return [dict objectForKey:key];
}

@end
